<template>
	<view class="content" >
        <u-navbar :fixed="true" :shadow="true" :safeAreaInsetTop="true" :placeholder="true"
            @leftClick="back" title="工人信息采集" >
        </u-navbar>

        <view class="u-m-t-30 u-m-b-30 u-flex u-row-center">
            <u-icon class="u-m-r-10" name="error-circle" color="#f0ad4e"></u-icon>
            <text class="u-font-26 uni-dec-color">请将采集信息输入下方框中</text>
        </view>

        <!-- 人员进场信息 -->
        <view class="u-p-t-30 u-p-b-50">
            <view class="uni-area">
                <uni-item-base-plus :baseData="info" @confirmSelect="confirmSelect" @inputChange="inputChange" @confirmDate="confirmDate" />
            </view>
        </view>

        <!-- 下一步 -->
        <view class="next-box u-flex u-flex-col u-row-center">
            <u-button class="pre" shape="circle" text="上一步" @tap="back"></u-button>
            <u-button class="next u-m-t-50" type="primary" shape="circle" text="提交采集" @tap="submit"></u-button>
        </view>

        <!-- 提示组件 -->
        <u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
import {mapGetters,mapMutations} from 'vuex'
import { upload } from '@/configs/uploadFile'
export default {
    data() {
        return {
            userInfo: uni.getStorageSync("userInfo"), // 用户信息
            parameter:{
                deptId: uni.getStorageSync("userInfo").deptId, // 所属部门编号
                gcbh: uni.getStorageSync("userInfo").ssgcbh // 所属项目(工程)编号
            },
            // 人员进场信息
            info:{
                type: 'info',
                line: false,
                title: '人员进场信息',
                rightText: '',
                list:[
                    { show:true, required:true, field:'bzmc', zdv:'', title:'班组名称', val:'', value:'',type:'select', pla:'请选择班组',data:[] },
                    // { field:'bzid',title:'班组编号' }, *
                    { show:true, required:true, field:'sfzz', zdv:'shifou', title:'是否班组长', val:'', value:'',type:'select', pla:'请选择是否班组长',data:[] },
                    { show:true, required:true, field:'gwlx', zdv:'role', title:'角色', val:'', value:'', type:'select', pla:'请选择角色',data:[] }, 
                    { show:true, required:true, field:'zwzc', zdv:'worktype', title:'从事工种', val:'', value:'', type:'select', pla:'请选择工种',data:[], data1:[] },           
                    // { field:'createRyid', title:'增加用户的人员编号' }, 
                    // { field:'createUser', title:'创建人' },
                ]
            },
        }
    },

    computed: {
        ...mapGetters(['autonymSubmitData']),
    },

    onReady(){},

    onLoad() {
        // 回填数据
        if( this.autonymSubmitData.id ){
            this.info.list.map((v,i)=>{
                v.value = this.autonymSubmitData[v.field]
            })
        }else{
            // 缓存其它要提交的数据
            this.setAutonymSubmitData({ 
                createRyid: this.userInfo.ssrybh,
                createUser: this.userInfo.acc+'['+this.userInfo.userName+']',
            })
        }
        // 获取班组列表
        this.getTeamList()
        // 获取下拉选择数据
        this.getSelectData()
    },

    methods: {
        ...mapMutations(['setAutonymSubmitData','emptyAutonymSubmitData']),

        back(){
            uni.navigateBack()
        },
        

        // 班组列表
        getTeamList(){
            this.$request(this,this.$apis.GroupListAll,this.parameter).then(res=>{
                // console.log('班组列表',res)
                this.info.list[0].data = []
                res.map((v,i)=>{
                    this.info.list[0].data.push({
                        field: 'bzmc',
                        label: v.bzmc,
                        value: v.id,
                        bzzxm: v.bzzxm
                    })
                })
                if( this.autonymSubmitData.id ){ // 当前班组已有班组长则不能再选择是班组长
                    let a = res.filter(e=>{ return e.id == this.autonymSubmitData.bzid })
                    this.setAutonymSubmitData({ __bzzxm: a[0].bzzxm })
                }
            }).then()
        },
        // 下拉选择数据
        getSelectData(){
            const arr = []
            this.info.list.map((v,i)=>{
                if( v.field!='bzmc' ) arr.push({ field:v.field, title:v.title, zdv: v.zdv, index: i})
            })
            let gwlxTemporary = [] // 角色
            arr.map((v,i)=>{
                let api = JSON.parse(JSON.stringify(this.$apis.CommonDictionaries))
                api.url += v.zdv
                this.$request(this,api,{}).then(res=>{
                    let resData = res
                    // 角色只取1025和1026
                    if( v.field == 'gwlx' ){ 
                        resData = []
                        res.map((a,b)=>{
                            if( a.dictValue == 1025 || a.dictValue == 1026 ) {
                                resData.push(a)
                            }
                        })
                        gwlxTemporary = resData
                    }
                    // 从事工种
                    if( v.field == 'zwzc' ){
                        resData = []
                        let zwzcArr = [], gwlx = 1025
                        if( this.autonymSubmitData.id ){
                            gwlx = this.autonymSubmitData.gwlx
                        }else{
                            gwlx = gwlxTemporary[0]?gwlxTemporary[0].dictValue:1025
                        }
                        if( gwlx == 1025 ){
                            zwzcArr = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '13', '16', '32', '64', '128', '129', '130', '131', '132' , '133', '134', '135', '136', '137', '138', '139', '142', '143', '144', '145']
                        }else{ 
                            zwzcArr = ['140', '141', '146', '147', '148', '149', '151', '152', '153', '154', '155']
                        }
                        res.map((a,b)=>{
                            zwzcArr.map((c,d)=>{
                                if(a.dictValue == c) {
                                    resData.push(a)
                                }
                            })
                        })
                        this.info.list[3].data1 = res
                    }
                    // 给下拉选择框放入数据
                    this.info.list[v.index].data = []
                    resData.map((c,d)=>{
                        this.info.list[v.index].data.push({
                            field: v.field,
                            label: c.dictLabel,
                            value: c.dictValue,
                            index: v.index
                        })
                    })
                    // 设置下拉选择框的默认值
                    if( this.autonymSubmitData.id ){
                        let a = resData.filter(e=>{ return e.dictValue == this.autonymSubmitData[v.field] })
                        this.info.list[v.index].value = a[0].dictLabel
                    }else{
                        if( v.field != 'sfzz'){
                            this.info.list[v.index].value = resData[0].dictLabel
                            this.info.list[v.index].val = resData[0].dictValue
                        }else{
                            this.info.list[v.index].value = resData[1].dictLabel
                            this.info.list[v.index].val = resData[1].dictValue
                        }
                        // 保存默认数据
                        this.setAutonymSubmitData({ [v.field]:this.info.list[v.index].val})
                    }
                }).then()
            })
            // 保存默认值
            if( !this.autonymSubmitData.id ) this.saveDefaultData()
        },
        // 保存默认数据
        saveDefaultData(){
            const obj = {}
            this.info.list.map((v,i)=>{
                if( v.field == 'bzmc' ){
                    obj[v.field] = v.value
                }else{ 
                    obj[v.field] = v.val
                }
            })
            this.setAutonymSubmitData(obj)
        },


        // 下拉选择确认
        confirmSelect(e){
            // 当前班组已班组长则不能再选择是班组长
            if( e.field == 'sfzz' && e.value == 1 ){
                if( this.autonymSubmitData.__bzzxm ){
                    uni.$u.toast('当前班组已有班组长')
                    return false
                }
            }
            // 角色值的改变
            if( e.field == 'gwlx' ){
                let zwzcTemporary = [], zwzcArr = []
                if( e.value == 1025 ){
                    zwzcArr = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '13', '16', '32', '64', '128', '129', '130', '131', '132' , '133', '134', '135', '136', '137', '138', '139', '142', '143', '144', '145']
                }else{ 
                    zwzcArr = ['140', '141', '146', '147', '148', '149', '151', '152', '153', '154', '155']
                }
                this.info.list[3].data1.map((a,b)=>{
                    zwzcArr.map((c,d)=>{
                        if(a.dictValue == c){
                            zwzcTemporary.push({
                                field: 'zwzc',
                                label: a.dictLabel,
                                value: a.dictValue
                            })
                        }
                    })
                })
                this.info.list[3].data = zwzcTemporary
                this.info.list[3].value = zwzcTemporary[0].label
                this.info.list[3].val = zwzcTemporary[0].value
                this.setAutonymSubmitData({ zwzc:zwzcTemporary[0].value })
            }
            // 视图赋值
            this.info.list.map((v,i)=>{
                if( v.field == e.field ) v.value = e.label 
            })
            // 要保存的数据赋值
            if( e.field == 'bzmc' ){
                this.setAutonymSubmitData({ [e.field]: e.label, bzid: e.value, __bzzxm:e.bzzxm }) 
            }else{
                this.setAutonymSubmitData({ [e.field]: e.value }) 
            }
        },
        // 输入事件
        inputChange(e){
            this.info.list.map((v,i)=>{
                if( v.field == e.field ) v.value = e.value
            })
            this.setAutonymSubmitData({ [e.field]: e.value })
        },
        // 时间按选择确认
        confirmDate(e){
            this.info.list.map((v,i)=>{
                if( v.field == e.field ) v.value = this.$moment(new Date(e.value)).format('YYYY-MM-DD')
            })
            this.setAutonymSubmitData({ [e.field]: this.$moment(new Date(e.value)).format('YYYY-MM-DD') })
        },


        // 提交
        submit(){
            // console.log('当前提交数据',this.autonymSubmitData)
            uni.showLoading({ title:'提交中' })
            let api 
            if( !this.autonymSubmitData.id ){
                api = this.$apis.autonymSave
            }else{
                api = this.$apis.autonymEdit
                this.setAutonymSubmitData({ rybh: this.userInfo.ssrybh  })
            }
            if( !this.autonymSubmitData.bzid ){ // 班组
                uni.$u.toast('请选择班组')
                return false
            }
            this.$request(this,api,this.autonymSubmitData).then(res=>{
                if( res.code == 200 ){
                    if( !this.autonymSubmitData.id ){
                        uni.showLoading({ title:'证件近照上传中'})
                        const arr = [
                            { type: '__front', attachType:'sfz' },
                            { type: '__back', attachType:'sfz' },
                            { type: '__photo', attachType:'ryjz' },
                        ]
                        arr.map((v,i)=>{
                            this.uploadImg(v,res.ryxx.id)
                        })
                    }else{
                        this.emptyAutonymSubmitData()
                        uni.navigateBack({delta:4})
                    }
                }else{
                    uni.$u.toast(res.msg)
                }
            })
        },
        // 上传身份证正反面以及近照
        uploadImg(v,bid){
            const parameter = {
                ywlx: 'salary_smzgl_ryxx_smrz',
                bid: bid,
                attachType: v.attachType
            }
            upload( this.autonymSubmitData[v.type],parameter ).then(res=>{
                if( v.type == '__photo' ){
                    uni.hideLoading()
                    this.emptyAutonymSubmitData()
                    uni.navigateBack({delta:4})
                }
            })
        },
    }
}
</script>

<style scoped lang="scss">
    .content{
        background: $uni-text-color-inverse;
        .next-box{
            padding: 116rpx 0 70rpx;
            .pre,.next{
                width: 500rpx;
                margin: 0;
            }
            .pre{
                background: #F6F7F8;
                border: none;
                /deep/.u-button__text{
                    color: #ADB4BD !important;
                }
            }
        }
    }
</style>
